
var timeOut = null;
var hasSelect = false;
var itemIndex = -1;
var maxItem = 0;
var callFun = '';
var trColor = '#e7f3fd';
var trSelect = '#fceba8';
var tableBg = '#80b9e9';

//边输入边弹出列表框
//必须参数: id:该控件id  table:要查询的表格  field:表字段
//非必须参数: where:查询条件  width:提示框宽度  itemCount:最多显示多少项,如果为0则显示全部  itemLength:每项的最大字串长度  callback:回调函数，参数为该控件id
function showListTip(id, table, field, where, width, itemCount, itemLength, callback)
{
	var e = window.event;
	if('13' == e.keyCode || '37' == e.keyCode || '38' == e.keyCode || '39' == e.keyCode || '40' == e.keyCode)
	{
		upOrDown(id);
		return;
	}
	if(where == undefined || where == null)
	{
		where = '';
	}
	if(callback != undefined && callback != null)
	{
		callFun = callback;
	}
	if(timeOut != null)
	{
		clearTimeout(timeOut);
	}
	var divTipObj = $('#divTip');
	var tt = $('#' + id);
	if('' == tt.val().Trim())
	{
		divTipObj.css('display', 'none');
		return;
	}
	if(itemCount == undefined || itemCount == null)
	{
		itemCount = 0;
	}
	tt.bind('blur', function(){
		hiddenDivTipObj();
	});
	timeOut = setTimeout('queryTip("' + id + '", "' + table + '", "' + field + '", "' + where + '", ' + width + ', "' + tt.val() + '", "' + itemCount + '", "' + itemLength + '")', 1000);
}

//查询
function queryTip(id, table, field, where, width, value, itemCount, itemLength)
{
	itemIndex = -1;
	var divTipObj = $('#divTip');
	var tt = $('#' + id);
	listTipService.queryTip(table, field, where, value, itemCount, function(data){
		if(width == undefined || width == null)
		{
			width = 200;
		}
		var len = data.length;
		maxItem = len;
		var h = '<table id="itemTable" style="position:absolute; z-index:12;" cellpadding="3" cellspacing="1" bgcolor="' + tableBg + '" width="' + width + '">';
		for(var i = 0; i < len; i++)
		{
			h += '<tr id="itemId' + i + '" style="cursor:hand" onclick="selectTipItem(\'' + id + '\', ' + i + ')" onmouseover="trOver(this, ' + i + ')" onmouseout="trOut(this)" bgcolor="' + trColor + '">';
			h += '<td style="word-break:break-all" height="22px">';
			var content = data[i][field];
			if(itemLength != undefined && content.length > itemLength)
			{
				content = content.substring(0, itemLength) + '...';
			}
			h += content;
			h += '<input id="tipItemHidden' + i + '" type="hidden" value="' + data[i][field] + '" /></td></tr>';
		}
		h += '</table>';
		divTipObj.css('display', 'block');
		divTipObj.html(h);
		var itemTable = $('#itemTable');
		var itemTableHeight = itemTable.height();
		var frame = '<iframe style="position:absolute; z-index:8;" width="' + width + 'px" height="' + itemTableHeight + 'px" scrolling="no" frameborder="0"></iframe>';
		divTipObj.html(divTipObj.html() + frame);
		var ttLeft = tt.offset().left;
		var ttTop = tt.offset().top + tt.height() + 6;
		divTipObj.css({left:ttLeft, top:ttTop});
	});
}

function selectHiddenDivTipObj()
{
	var divTipObj = $('#divTip');
	divTipObj.css('display', 'none');
	resetItemParam();
}

function selectTipItem(id, x)
{
	hasSelect = true;
	var tipItemHiddenx = $('#tipItemHidden' + x);
	var tt = $('#' + id);
	tt.val(tipItemHiddenx.val());
	var divTipObj = $('#divTip');
	divTipObj.css('display', 'none');
	resetItemParam();
	if('' != callFun)
	{
		eval(callFun + '("' + id + '")');
	}
}

function trOver(obj, x)
{
	var itemIdx = $('#itemId' + itemIndex);
	itemIdx.css('backgroundColor', trColor);
	obj.style.backgroundColor = trSelect;
	itemIndex = x;
}

function trOut(obj)
{
	obj.style.backgroundColor = trColor;
}


//上下事件
function upOrDown(id)
{
	var e = window.event;
	var divTipObj = $('#divTip');
	if('38' == e.keyCode)
	{
		if(itemIndex == -1)
			return;
		var itemIdBefore = $('#itemId' + itemIndex);
		itemIdBefore.css('backgroundColor', trColor);
		itemIndex -= 1;
		var itemIdx = $('#itemId' + itemIndex);
		if(itemIdx.length != 0)
		{
			itemIdx.css('backgroundColor', trSelect);
		}
	}
	else if('40' == e.keyCode)
	{
		if(itemIndex == maxItem)
			return;
		var itemIdBefore = $('#itemId' + itemIndex);
		if(itemIdBefore.length != 0)
		{
			itemIdBefore.css('backgroundColor', trColor);
		}
		itemIndex += 1;
		var itemIdx = $('#itemId' + itemIndex);
		itemIdx.css('backgroundColor', trSelect);
	}
	else if('13' == e.keyCode && 'block' == divTipObj.css('display'))
	{
		hasSelect = true;
		if(itemIndex != -1 && itemIndex < maxItem)
		{
			var tt = $('#' + id);
			var tipItemHiddenx = $('#tipItemHidden' + itemIndex);
			tt.val(tipItemHiddenx.val());
			selectHiddenDivTipObj();
			if('' != callFun)
			{
				eval(callFun + '("' + id + '")');
			}
		}
	}
}

//隐藏div提示
function hiddenDivTipObj()
{
	setTimeout('selectHiddenDivTipObj()', 300);
}

//还原
function resetItemParam()
{
	itemCount = 0;
	itemIndex = -1;
}